package com.imooc.ecommerce.advice;

import com.imooc.ecommerce.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * ClassName: GlobalExceptionAdvice
 * Package: com.imooc.ecommerce.advice
 *
 * @Author 马学兴
 * @Create 2024/3/25 10:33
 * @Version 1.0
 * Description:全局异常捕获处理
 */
@Slf4j
@RestControllerAdvice//它是 @ControllerAdvice 和 @ResponseBody 注解的组合。
// 它的作用是在全局范围内进行异常处理、全局数据绑定以及模型增强等操作
public class GlobalExceptionAdvice {
    @ExceptionHandler(value = Exception.class)
    public CommonResponse<String> handleCommerceException(HttpServletRequest req,Exception ex){
        CommonResponse<String> response=new CommonResponse<>(
                -1,"business error"
        );
        response.setData(ex.getMessage());
        log.error("commerce service has error:[{}]",ex.getMessage(),ex);
        return response;
    }
}
